package com.jms.web.controller.jms.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jms.common.core.domain.AjaxResult;
import com.jms.common.core.domain.entity.SysDept;
import com.jms.common.filter.utils.DateUtils;
import com.jms.common.filter.utils.SecurityUtils;
import com.jms.common.filter.utils.StringUtils;
import com.jms.system.mapper.SysDeptMapper;
import com.jms.web.controller.jms.domain.Enterprise;
import com.jms.web.controller.jms.mapper.EnterpriseMapper;
import com.jms.web.controller.jms.service.IEnterpriseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 企业Service业务层处理
 *
 * @author drx
 * @date 2024-04-29
 */
@Service
public class EnterpriseServiceImpl implements IEnterpriseService {
    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询企业
     *
     * @param id 企业主键
     * @return 企业
     */
    @Override
    public Enterprise selectEnterpriseById(Long id) {
        Enterprise enterprise = enterpriseMapper.selectEnterpriseById(id);
        if (StringUtils.isNotEmpty(enterprise.getAreaCode())) {
            String areaName = "";
            List<Long> arealist = JSONArray.parseArray(enterprise.getAreaCode(), Long.class);

            for (int i = 0; i < arealist.size(); i++) {
                SysDept sysDept = sysDeptMapper.selectDeptById(arealist.get(i));
                if (!Objects.isNull(sysDept)) {
                    areaName += sysDept.getDeptName() + "/";
                }
            }

            if (!"".equals(areaName)) {
                enterprise.setAreaName(areaName.substring(0, areaName.length() - 1));
            }

        }

        return enterprise;
    }

    /**
     * 查询企业列表
     *
     * @param enterprise 企业
     * @return 企业
     */
    @Override
    public List<Enterprise> selectEnterpriseList(Enterprise enterprise) {
        List<Enterprise> enterprises = enterpriseMapper.selectEnterpriseList(enterprise);

        /*转译区划编码*/
        if (!Objects.isNull(enterprises)) {
            enterprises.forEach(bean -> {

                if (StringUtils.isNotEmpty(bean.getAreaCode())) {
                    String areaName = "";
                    List<Long> arealist = JSONArray.parseArray(bean.getAreaCode(), Long.class);

                    for (int i = 0; i < arealist.size(); i++) {
                        SysDept sysDept = sysDeptMapper.selectDeptById(arealist.get(i));
                        if (!Objects.isNull(sysDept)) {
                            areaName += sysDept.getDeptName() + "/";
                        }
                    }

                    if (!"".equals(areaName)) {
                        bean.setAreaName(areaName.substring(0, areaName.length() - 1));
                    }

                }

            });
        }

        return enterprises;
    }

    /**
     * 新增企业
     *
     * @param enterprise 企业
     * @return 结果
     */
    @Override
    public int insertEnterprise(Enterprise enterprise) {
        enterprise.setCreateTime(DateUtils.getNowDate());
        enterprise.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
        enterprise.setCreateBy(String.valueOf(SecurityUtils.getUserId()));

        if (StringUtils.isNotEmpty(enterprise.getAreaCode())){
            List<Long> areacodes = JSONArray.parseArray(enterprise.getAreaCode(), Long.class);
            if (areacodes.size() > 0){
                Long code = areacodes.get(areacodes.size() - 1);
                SysDept sysDept = sysDeptMapper.selectDeptById(code);

                enterprise.setAreaShortCode(null != sysDept ? sysDept.getAreaShortCode() : null);
            }
        }

        return enterpriseMapper.insertEnterprise(enterprise);
    }

    /**
     * 修改企业
     *
     * @param enterprise 企业
     * @return 结果
     */
    @Override
    public int updateEnterprise(Enterprise enterprise) {
        enterprise.setUpdateTime(DateUtils.getNowDate());
        enterprise.setUpdateName(SecurityUtils.getLoginUser().getUser().getNickName());
        enterprise.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));

        if (StringUtils.isNotEmpty(enterprise.getAreaCode())){
            List<Long> areacodes = JSONArray.parseArray(enterprise.getAreaCode(), Long.class);
            if (areacodes.size() > 0){
                Long code = areacodes.get(areacodes.size() - 1);
                SysDept sysDept = sysDeptMapper.selectDeptById(code);

                enterprise.setAreaShortCode(null != sysDept ? sysDept.getAreaShortCode() : null);
            }
        }

        return enterpriseMapper.updateEnterprise(enterprise);
    }

    /**
     * 批量删除企业
     *
     * @param ids 需要删除的企业主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseByIds(Long[] ids) {
        return enterpriseMapper.deleteEnterpriseByIds(ids);
    }

    /**
     * 删除企业信息
     *
     * @param id 企业主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseById(Long id) {
        return enterpriseMapper.deleteEnterpriseById(id);
    }

    @Override
    public AjaxResult getEnterpriseDict() {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<Enterprise> list = enterpriseMapper.getEnterpriseDict();
            ajaxResult.put("data", list);
            ajaxResult.put("code", 200);
            ajaxResult.put("msg", "获取企业字典成功");
        } catch (Exception e) {
            ajaxResult.put("data", null);
            ajaxResult.put("code", 500);
            ajaxResult.put("msg", "获取企业字典失败，请联系管理员");
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
